package com.sgcc.ln.pmos.e_ln_home.activity;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.ln.pmos.arouter.common.CommonArouterPath;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.common.base.BaseActivity;
import com.sgcc.ln.pmos.common.http.DownloadUtil;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.e_ln_home.adapter.PublicFileItemAdapter;
import com.sgcc.ln.pmos.e_ln_home.bean.DynamicNetBean;

import java.io.File;


/**
 * 市场动态 政策法规 详情页面
 */
@Route(path = HomeArouterPath.HOME_PUBLICLAWSDYNAMICINFO)
public class PublicLawsDynamicInfoActivity extends BaseActivity {

    private TitleBar mTitleBar;
    private TextView mTvTitle;
    private TextView mTvEye;
    private TextView mTvTime;
    private TextView mTvContent;
    private RecyclerView mRvContent;
    private LinearLayoutManager mLayoutManager;
    private PublicFileItemAdapter mPublicFileItemAdapter;

    @Autowired
    public DynamicNetBean.DataDTO.ListDTO mDynamicBean;

    @Autowired
    public String from; //1政策法规  2市场动态

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_publiclawsdynamicinfo;
    }

    @Override
    protected void initViews() {
        super.initViews();
        findViewByIds();

        String title = "1".equals(from) ? "政策法规" : "市场动态";
        mTitleBar.setTitle(title);
        mTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });

        mTvTitle.setText(mDynamicBean.getTitle());

//        int number = SPUtils.getInstance(mDynamicBean.getTitle_Id()).getInt("number");
        mTvEye.setText(mDynamicBean.getReadAmount() + "浏览");
        mTvTime.setText(mDynamicBean.getOperateDate());
        mTvContent.setText(mDynamicBean.getContent());

        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRvContent.setLayoutManager(mLayoutManager);
        mRvContent.setItemAnimator(new DefaultItemAnimator());
        mPublicFileItemAdapter = new PublicFileItemAdapter(this);
        mRvContent.setAdapter(mPublicFileItemAdapter);
        mPublicFileItemAdapter.setOnItemClickListener(new PublicFileItemAdapter.OnItemClickListener() {
            @Override
            public void itemClickListener(int position, DynamicNetBean.DataDTO.ListDTO.Attachment bean) {

                download(bean.getId());
            }
        });
        mPublicFileItemAdapter.setNewData(mDynamicBean.getAttachmentList());

    }

    private void findViewByIds() {
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvEye = (TextView) findViewById(R.id.tv_eye);
        mTvTime = (TextView) findViewById(R.id.tv_time);
        mTvContent = (TextView) findViewById(R.id.tv_content);
        mRvContent = (RecyclerView) findViewById(R.id.rv_content);
    }


    private void download(String id) {
        showLoading();

        DownloadUtil.get().downloadPdf(this, id, new DownloadUtil.OnDownloadListener() {
            @Override
            public void onDownloadSuccess(File file) {
                dismissLoading();
                ARouter.getInstance()
                        .build(CommonArouterPath.COMMON_PDFPREVIEW)
                        .withString("mFilePath", file.getAbsolutePath())
                        .navigation();
            }

            @Override
            public void onDownloading(int progress) {

            }

            @Override
            public void onDownloadFailed(Exception e) {
                dismissLoading();
                ToastUtils.showLong("文件下载失败");
            }
        });

    }


}
